package com.alfayun.fiscalriskanalysis.reportspec;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ReportSpecService {
	
	@Autowired
	private ReportSpecRepository repository;
	
	public List<ReportSpec> list() {
		return repository.findAll();
	}
	
	public ReportSpec queryById(int id) throws NoSuchReportSpecException {
		return repository.findById(id).orElseThrow(NoSuchReportSpecException::new);
	}
	
	public ReportSpec newReportSpec(ReportSpec reportSpec) {
		return ReportSpec.newReportSpec(repository, reportSpec);
	}

	public void deleteReportSpec(int id) {
		repository.deleteById(id);
	}

	public ReportSpec modifyReportSpec(int id, ReportSpec reportSpec) throws NoSuchReportSpecException {
		Optional<ReportSpec> row = repository.findById(id);

		if (row.isPresent()) {
			return repository.save(reportSpec);
		}

		throw new NoSuchReportSpecException();
	}

	public Object getFinancialReports() {
		return repository.findByReportType(ReportType.FINANCIAL_STATEMENTS);
	}

}
